package com.hrm.controller.org;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.hrm.model.dto.org.OrgBaseDTO;
import com.hrm.model.dto.org.TreeDTO;
import com.hrm.model.service.OrgService;

public class OrgBaseInfoController extends AbstractController{
	
	private OrgService orgService;

	public void setOrgService(OrgService orgService) {
		this.orgService = orgService;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		String orgcode = arg0.getParameter("code");
		orgcode = (orgcode==null)? "01000000":orgcode;
		String sdate = arg0.getParameter("sdate");
		if(sdate==null){
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH)+1;
			int date = cal.get(Calendar.DATE);
			sdate = (year+"-"+month+"-"+date);
		}

		String orgtree = orgService.getMenuTreeString(sdate).toString(); 		
		ModelAndView mv = new ModelAndView();
		
		OrgBaseDTO dto = orgService.getOrgbase(orgcode);
		String orguppername; 
		if(dto.getOrgcode().equals("01000000")){
			orguppername = "";			
		} else {
			orguppername = orgService.getOrgbase(dto.getOrguppercode()).getOrgname();			
		}
		
		List<TreeDTO> treelist = orgService.getOrgtree(sdate);
		mv.addObject("orgcontroller","1");
		mv.addObject("treelist", treelist);
		mv.addObject("orguppername",orguppername);
		mv.addObject("orgtree",orgtree);
		mv.addObject("orgbaseinfo", dto);
		mv.addObject("sdate", sdate);
		mv.setViewName("orgbaseinfo");

		return mv;
	}
	

}
